/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeans;

import entities.Account;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.transaction.RollbackException;
import sessionBeans.AccountFacade;

/**
 *
 * @author kienNT
 */
@ManagedBean(name = "registeraccount")
@RequestScoped
public class RegisterAccount {

    @EJB
    private AccountFacade accountFacade;
    private String username;
    private String password;
    private String email;
    private String firstname;
    private String lastname;
    private String address;
    private String phone;
    private boolean subcribe;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public boolean isSubcribe() {
        return subcribe;
    }

    public void setSubcribe(boolean subcribe) {
        this.subcribe = subcribe;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    /** Creates a new instance of RegisterAccount */
    public RegisterAccount() {
    }

    public void InsertUser() {
        try {
            Account ac = new Account(username, password, email, firstname, lastname, address, phone, subcribe);
            accountFacade.create(ac);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Register completed"));
            reset();
        } catch (Throwable e) {
            while (e.getCause() != null) {
                e = e.getCause();
            }
            String message = e.getMessage();
            if (message.contains("IX_Account_username_unique")) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "This username is already used. Please choose another one.", null));
            } else if (message.contains("IX_Account_email_unique")) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "This email is already used. Please choose another one.", null));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), null));
            }
        }

    }

    private void reset() {
        username = "";
        password = "";
        email = "";
        firstname = "";
        lastname = "";
        address = "";
        phone = "";
        subcribe = true;
    }
}
